package com.yjd.bi.statistic.control;

import com.yjd.bi.birthday.service.IBirthdayService;
import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.comm.annotation.DateString;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import com.yjd.comm.util.ServletUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.Map;

/**
 * 生日礼包统计
 * Created by yt on 2016/12/14.
 */
@Controller
@Scope("prototype")
@RequestMapping("/birthday")
public class BirthdayControl extends BaseControl{

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    IBirthdayService birthdayService;

    @RequestMapping("/index")
    public String index (@DateString String start, @DateString String end)throws Exception{
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("now", BiDateUtil.initBiDate());
        return "birthday.index";
    }


    /**
     * 总览
     *
     * @throws Exception
     *             void
     */
    @RequestMapping("/summary")
    public void summary(@DateString String start, @DateString String end)
            throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("start", FrameUtil.getTime(DateUtil.parseDate(start,DateUtil.PATTERN_yyyyMMdd)));
        paramMap.put("end", FrameUtil.getTime(DateUtil.parseDate(end,DateUtil.PATTERN_yyyyMMdd)));
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map firstRechMap = birthdayService.getFirstRech(paramMap);
        Double receiveCount = birthdayService.getValueBySqlName("receiveCount",paramMap);
        Double userCount = birthdayService.getValueBySqlName("userCount",paramMap);
        Double fisrtRechCount =Double.parseDouble( firstRechMap.get("count").toString());
        Double rechCount = birthdayService.getValueBySqlName("rechCount",paramMap);
        Double investCount = birthdayService.getValueBySqlName("investCount",paramMap);
        Double fisrtRechPrice = Double.parseDouble(firstRechMap.get("price").toString());
        Double rechPrice = birthdayService.getValueBySqlName("rechPrice",paramMap);
        Double investPrice = birthdayService.getValueBySqlName("investPrice",paramMap);
        //召回
        Double recallCount = birthdayService.getValueBySqlName("recallCount",paramMap);
        //首投
        Double firstInvest = birthdayService.getValueBySqlName("firstInvest",paramMap);
        super.successData(FrameUtil.newHashMap("summaryVals", FrameUtil.newArrayList(
                NumberUtil.formatNumber(receiveCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(userCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(fisrtRechCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(rechCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(investCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(fisrtRechPrice, NumberUtil.FORMAT_4),
                NumberUtil.formatNumber(rechPrice, NumberUtil.FORMAT_4),
                NumberUtil.formatNumber(investPrice, NumberUtil.FORMAT_4),
                NumberUtil.formatNumber(recallCount,NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(firstInvest,NumberUtil.FORMAT_2))));
    }
}
